package no.kolstad.movies.model;
/**
 *  Copyright [2008] Christopher Kolstad
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License. 
 */

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 *
 * @author chriswk
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@NamedQuery(name="movies.for.user", query="FROM Movie m WHERE m.owner = :user")
public class Movie extends BaseModelObject implements Serializable {

    private Long id;
    private Long version;
    private String title;
    private Long prodYear;
    private User owner;
    private Lender lender;
    
    @Id @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    
    @Version
    public Long getVersion() {
        return version;
    }
    
    public void setVersion(Long version) {
        this.version = version;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public Long getProdYear() {
        return prodYear;
    }
    public void setProdYear(Long year) {
        this.prodYear = year;
    }
    
    @ManyToOne
    public Lender getLender() {
        return lender;
    }
    
    public void setLender(Lender lend) {
        this.lender = lend;
    }
    @Transient
    public boolean isAvailable() {
        if(lender != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    
    @ManyToOne
    public User getOwner() {
      return owner;
    }
    public void setOwner(User owner) {
      this.owner = owner;
    }
    
    @Override
    public boolean equals(Object o) {
    if (this == o)
      return true;

    if (o == null || !o.getClass().equals(this.getClass())
        || this.getId() == null)
      return false;

    return this.getId().equals(((BaseModelObject) o).getId());
  }
}
